﻿using System.Data;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Cart.StateProviders
{
    public class CartSQLStateProvider : CartStateProvider, ICartStateProvider
    {
        public Cart CurrentCart
        {
            get
            {
                object key = HttpContext.Current.Session["CART_KEY"];
                if (key == null) { return null; }
                return GetCart(key.ToString());
            }
            set { SaveCart(value); }
        }

        public Cart CreateCart()
        {
            Cart cart = new Cart();
            HttpContext.Current.Session.Add("CART_KEY", cart.ID);
            AddCart(cart);
            return cart;
        }

        private bool CartExists(Cart Cart)
        {
            Marbles.Data.DataSource dbc = new Marbles.Data.DataSource("SessionState");
            Marbles.Data.DataSourceCommand cmd = new Marbles.Data.DataSourceCommand("SELECT cartid FROM tblCart WHERE cartid = @cartid");
            cmd.Parameters.Add("@cartid", Cart.ID);
            DataTable dt = dbc.FetchDataTable(cmd);
            return (dt.Rows.Count > 0);
        }

        public void DestroyCart(Cart Cart)
        {
            Marbles.Data.DataSource dbc = new Marbles.Data.DataSource("SessionState");
            Marbles.Data.DataSourceCommand cmd = new Marbles.Data.DataSourceCommand("DELETE FROM tblCart WHERE cartid = @cartid");
            cmd.Parameters.Add("@cartid", Cart.ID);
            dbc.ExecuteCommand(cmd);
        }

        private void AddCart(Cart cart)
        {
            Marbles.Data.DataSource dbc = new Marbles.Data.DataSource("SessionState");
            Marbles.Data.DataSourceCommand cmd = new Marbles.Data.DataSourceCommand("DELETE FROM tblCart WHERE cartid = @cartid; INSERT INTO tblCart (cartid, cart, updatedate, creationdate) VALUES (@cartid, @cart, @updatedate, @creationdate)");
            cmd.Parameters.Add("@cartid", cart.ID);
            cmd.Parameters.Add("@cart", SerializeCart(cart));
            cmd.Parameters.Add("@updatedate", DateTime.Now);
            cmd.Parameters.Add("@creationdate", DateTime.Now);
            dbc.ExecuteCommand(cmd);
        }

        private bool SaveCart(Cart cart)
        {
            if (!CartExists(cart)) { AddCart(cart); return true; }
            Marbles.Data.DataSource dbc = new Marbles.Data.DataSource("SessionState");
            Marbles.Data.DataSourceCommand cmd = new Marbles.Data.DataSourceCommand("UPDATE tblCart SET cart = @cart, updatedate = @updatedate WHERE cartid = @cartid");
            cmd.Parameters.Add("@cartid", cart.ID);
            cmd.Parameters.Add("@cart", SerializeCart(cart));
            cmd.Parameters.Add("@updatedate", DateTime.Now);
            dbc.ExecuteCommand(cmd);
            return false;
        }

        private Cart GetCart(string key)
        {
            Marbles.Data.DataSource dbc = new Marbles.Data.DataSource("SessionState");
            Marbles.Data.DataSourceCommand cmd = new Marbles.Data.DataSourceCommand("SELECT cart FROM tblCart WHERE cartid = @cartid");
            cmd.Parameters.Add("@cartid", key);
            DataTable dt = dbc.FetchDataTable(cmd);

            if (dt.Rows.Count > 0)
            {
                return DeSerializeCart(dt.Rows[0]["cart"].ToString());
            }

            return null;
        }
    }
}
